# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :event_loop.py
# @Time      :2025/7/9 18:08
# @Author    :aiyimu

import asyncio

async def task(name, seconds):
    print(f"{name}开始执行, 将休眠{seconds}秒")
    await asyncio.sleep(seconds)
    print(f"{name}执行完毕")
    return f"{name}结束"

# 创建事件循环
loop = asyncio.get_event_loop()
# 创建多个任务
tasks = [
    loop.create_task(task("任务A", 2)),
    loop.create_task(task("任务B", 1)),
    loop.create_task(task("任务C", 4))
]

# 允许事件直至任务完成
results = loop.run_until_complete(asyncio.gather(*tasks))
print("所有任务均完成: ", results)

# 关闭事件循环
loop.close()